package ar.fiuba.logger;

import java.util.Date;

/**
 * The Class LogMessage.
 * 
 * This class implements the log message. It contains all data required for a log message.
 */
public class LogMessage implements LogMessageInterface {

  /** The date. */
  private Date date;

  /** The file name. */
  private String fileName;

  /** The line number. */
  private int lineNumber;

  /** The log level. */
  private LogLevel logLevel;

  /** The log message. */
  private String logMessage;

  /** The method name. */
  private String methodName;

  /** The thread name. */
  private String threadName;

  /** The logger name. */
  private String loggerName;

  /** The exception. */
  private Throwable exception;

  /**
   * Instantiates a new log message.
   * 
   * @param message the message
   * @param level the level
   * @param callerInfo the caller info
   * @param loggerName the logger name
   */
  public LogMessage(String message, LogLevel level, LogEnvironmentInformation callerInfo,
      String loggerName) {
    init(message, level, callerInfo, loggerName, null);
  }

  /**
   * Instantiates a new log message.
   * 
   * @param message the message
   * @param level the level
   * @param callerInfo the caller info
   * @param loggerName the logger name
   * @param exception the exception
   */
  public LogMessage(String message, LogLevel level, LogEnvironmentInformation callerInfo,
      String loggerName, Throwable exception) {
    init(message, level, callerInfo, loggerName, exception);
  }

  /**
   * Inits the LogMessage.
   * 
   * @param message the message
   * @param level the level
   * @param callerInfo the caller info
   * @param loggerName the logger name
   * @param exception the exception
   */
  private void init(String message, LogLevel level, LogEnvironmentInformation callerInfo,
      String loggerName, Throwable exception) {
    this.date = new Date();
    this.fileName = callerInfo.getFileName();
    this.lineNumber = callerInfo.getLineNumber();
    this.logLevel = level;
    this.logMessage = message;
    this.methodName = callerInfo.getMethodName();
    this.threadName = callerInfo.getThreadName();
    this.loggerName = loggerName;
    this.exception = exception;
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    LogMessage other = (LogMessage) obj;
    if (date == null) {
      if (other.date != null)
        return false;
    } else if (!date.equals(other.date))
      return false;
    if (fileName == null) {
      if (other.fileName != null)
        return false;
    } else if (!fileName.equals(other.fileName))
      return false;
    if (lineNumber != other.lineNumber)
      return false;
    if (logLevel != other.logLevel)
      return false;
    if (logMessage == null) {
      if (other.logMessage != null)
        return false;
    } else if (!logMessage.equals(other.logMessage))
      return false;
    if (loggerName == null) {
      if (other.loggerName != null)
        return false;
    } else if (!loggerName.equals(other.loggerName))
      return false;
    if (methodName == null) {
      if (other.methodName != null)
        return false;
    } else if (!methodName.equals(other.methodName))
      return false;
    if (threadName == null) {
      if (other.threadName != null)
        return false;
    } else if (!threadName.equals(other.threadName))
      return false;
    if (exception == null) {
      if (other.exception != null)
        return false;
    }
    return true;
  }

  /*
   * (non-Javadoc)
   * 
   * @see ar.fiuba.logger.LogMessageInterface#getDate()
   */
  @Override
  public Date getDate() {
    return date;
  }

  /*
   * (non-Javadoc)
   * 
   * @see ar.fiuba.logger.LogMessageInterface#getFileName()
   */
  @Override
  public String getFileName() {
    return fileName;
  }

  /*
   * (non-Javadoc)
   * 
   * @see ar.fiuba.logger.LogMessageInterface#getLineNumber()
   */
  @Override
  public int getLineNumber() {
    return lineNumber;
  }

  /*
   * (non-Javadoc)
   * 
   * @see ar.fiuba.logger.LogMessageInterface#getLoggerName()
   */
  @Override
  public String getLoggerName() {
    return loggerName;
  }

  /*
   * (non-Javadoc)
   * 
   * @see ar.fiuba.logger.LogMessageInterface#getLogLevel()
   */
  @Override
  public LogLevel getLogLevel() {
    return logLevel;
  }

  /*
   * (non-Javadoc)
   * 
   * @see ar.fiuba.logger.LogMessageInterface#getLogMessage()
   */
  @Override
  public String getLogMessage() {
    return logMessage;
  }

  /*
   * (non-Javadoc)
   * 
   * @see ar.fiuba.logger.LogMessageInterface#getMethodName()
   */
  @Override
  public String getMethodName() {
    return methodName;
  }

  /*
   * (non-Javadoc)
   * 
   * @see ar.fiuba.logger.LogMessageInterface#getThreadName()
   */
  @Override
  public String getThreadName() {
    return threadName;
  }

  /*
   * (non-Javadoc)
   * 
   * @see ar.fiuba.logger.LogMessageInterface#getException()
   */
  @Override
  public Throwable getException() {
    return exception;
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((date == null) ? 0 : date.hashCode());
    result = prime * result + ((fileName == null) ? 0 : fileName.hashCode());
    result = prime * result + lineNumber;
    result = prime * result + ((logLevel == null) ? 0 : logLevel.hashCode());
    result = prime * result + ((logMessage == null) ? 0 : logMessage.hashCode());
    result = prime * result + ((loggerName == null) ? 0 : loggerName.hashCode());
    result = prime * result + ((methodName == null) ? 0 : methodName.hashCode());
    result = prime * result + ((threadName == null) ? 0 : threadName.hashCode());
    result = prime * result + ((exception == null) ? 0 : 1);
    return result;
  }
}
